import onnx
from . import values

def analyze(onnxfile, **kwargs):
    model = onnx.load(onnxfile)

    dict_reshape = {}
    dict_expand = {}
    dict_reshape2 = {}

    ready = False

    for node_id, node in enumerate(model.graph.node):
        #print(node_id, ", name:", node.name, ", input:", node.input, ", output:", node.output,  \
        #        ", op:", node.op_type, ', len(input):', len(node.input))

        if node.op_type == 'Reshape':
            if ready == True:
                if dict_reshape and dict_expand and node.input[0] == dict_expand['output'][0]:
                    dict_reshape2['input'] = node.input
                    return False     
                else:
                    print('clear dict_reshape and dict_expand, dict_reshapes')
                    print('dict_reshape:', dict_reshape)
                    print('dict_expand:', dict_expand)
                    print('dict_reshape2:', dict_reshape2)
                    dict_reshape = {}
                    dict_expand = {}
                    dict_reshape2 = {}
                    ready = False 
            else:
                dict_reshape['input'] = node.input
                dict_reshape['output'] = node.output
                dict_reshape['id'] = node_id

                print('got match Reshape node:', node.name)
            
        if node.op_type == 'Expand':
            if dict_reshape and node.input[0] == dict_reshape['output'][0]:
                dict_expand['input'] = node.input
                dict_expand['output'] = node.output
                dict_expand['id'] = node_id

                ready = True

                print('got match Expand node:', node.name)
            else:
                print('clear dict_reshape:', dict_reshape)
                dict_reshape = {}
                ready = False       

    return True